package com.py.framework.service.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.alibaba.fastjson.JSON;
import com.py.framework.common.Constants;
import com.py.framework.common.TaskHelper;
import com.py.framework.core.exception.ServiceException;
import com.py.framework.core.helper.UrlHelper;
import com.py.framework.core.utils.DateUtils;
import com.py.framework.dao.TaskDao;
import com.py.framework.entity.TaskEntity;
import com.py.framework.service.TaskService;

/**
 * 任务管理Service实现
 * @author Leegern
 * @date   2018年4月22日
 */
@Service
public class TaskServiceImpl implements TaskService {
	
	@Autowired
	private TaskDao taskDao;
	@Autowired
	private UrlHelper urlHelper;
	
	
	/*
	 * (non-Javadoc)
	 * @see com.py.framework.service.TaskService#addTaskInfo(com.py.framework.entity.TaskEntity)
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public int addTaskInfo(TaskEntity entity) {
		int flag = 0;
		entity.setCreateTime(new Timestamp(System.currentTimeMillis()));
		entity.setUpdateTime(new Timestamp(System.currentTimeMillis()));
		entity.setDelFlag(Constants.NUMBER_ONE);
		entity.setTaskStatus(Constants.NUMBER_TWO);
		flag = taskDao.addTaskInfo(entity);
		// 发送任务信息
		this.sendTask(entity);
		return flag;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.py.framework.service.TaskService#addTaskInfo(com.py.framework.entity.TaskEntity)
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public int addTaskInfoForXypj(TaskEntity entity) {
		taskDao.deleteTaskInfoForXypj(entity.getMenuId(), entity.getTaskUrl());
		int flag = 0;
		entity.setCreateTime(new Timestamp(System.currentTimeMillis()));
		entity.setUpdateTime(new Timestamp(System.currentTimeMillis()));
		entity.setDelFlag(Constants.NUMBER_ONE);
		entity.setTaskStatus(Constants.NUMBER_TWO);
		flag = taskDao.addTaskInfo(entity);
		this.sendTask(entity);
		this.updateTaskStatus(entity.getId(),Constants.NUMBER_ONE);
		// 发送任务信息
		return flag;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.py.framework.service.TaskService#queryTaskListForPage(java.util.Map)
	 */
	@Override
	public List<TaskEntity> queryTaskListForPage(Map<String, Object> params) {
		if (! CollectionUtils.isEmpty(params)) {
			return taskDao.queryTaskListForPage(params);
		}
		return null;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.py.framework.service.TaskService#findOneTask(long)
	 */
	@Override
	public TaskEntity findOneTask(long id) {
		if (id > Constants.NUMBER_ZERO) {
			return taskDao.findOneTask(id);
		}
		return null;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.py.framework.service.TaskService#updateTaskInfo(com.py.framework.entity.TaskEntity)
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public int updateTaskInfo(TaskEntity entity) {
		int flag = 0;
		entity.setUpdateTime(new Timestamp(System.currentTimeMillis()));
		flag = taskDao.updateTaskInfo(entity);
		TaskEntity task = this.findOneTask(entity.getId());
		if (null != task) {
			task.setTaskUrl(entity.getTaskUrl());
			task.setCronExp(entity.getCronExp());
			task.setUpdFlag(Constants.NUMBER_TWO);
			// 发送任务信息
			this.sendTask(task);
		}
		return flag;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.py.framework.service.TaskService#updateTaskStatus(long, int)
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public int updateTaskStatus(long id, int taskStatus) {
		int flag = 0;
		if (id > Constants.NUMBER_ZERO && taskStatus > Constants.NUMBER_ZERO) {
			TaskEntity entity = this.findOneTask(id);
			flag = taskDao.updateTaskStatus(id, new Timestamp(System.currentTimeMillis()), taskStatus);
			if (null != entity) {
				// 发送任务信息
				entity.setTaskStatus(taskStatus);
				this.sendTask(entity);
			}
		}
		return flag;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.py.framework.service.TaskService#batchDelTasks(java.util.List)
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public int batchDelTasks(List<Long> ids) {
		int flag = 0;
		if (! CollectionUtils.isEmpty(ids)) {
			List<TaskEntity> datas = new ArrayList<>();
			TaskEntity entity = null;
			for (Long id : ids) {
				entity = this.findOneTask(id);
				// 判断任务是否是开启状态
				if (null != entity && entity.getTaskStatus() == Constants.NUMBER_ONE) {
					throw new ServiceException(entity.getTaskName() + "任务是开启状态，删除失败！");
				}
				datas.add(entity);
			}
			Map<String, Object> params = new HashMap<>();
			params.put("updateTime", new Timestamp(DateUtils.getCurrentDateTime().getTime()));
			params.put("ids",        ids);
			flag = taskDao.batchDelTasks(params);
			
			for (TaskEntity taskEntity : datas) {
				if (null != taskEntity) {
					// 发送任务信息
					taskEntity.setDelFlag(Constants.NUMBER_TWO);
					this.sendTask(taskEntity);
				}
			}
		}
		return flag;
	}
	
	/**
	 * 发送任务信息
	 * @param task 任务实体
	 */
	private void sendTask(TaskEntity task) {
		// 异步发送任务信息
		TaskHelper.postAsyncTask(urlHelper.getSchedulerServerUrl() + Constants.SCHEDULER_PATH, JSON.toJSONString(task));
	}

	@Override
	public int updateTaskStatusForXypj(String status, long menuId, String taskUrl) {
		// TODO Auto-generated method stub
		TaskEntity entity=taskDao.findOneTaskForXypj(menuId, taskUrl);
		int flag = taskDao.updateTaskStatus(entity.getId(), new Timestamp(System.currentTimeMillis()), Integer.parseInt(status));
		if (null != entity) {
			// 发送任务信息
			entity.setTaskStatus(Integer.parseInt(status));
			this.sendTask(entity);
		}
		return flag;
	}
	
}